<template>
  <div class="mod-webConfig-webConfig">
    <avue-crud ref="crud"
               :data="dataList"
               :page="page"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @on-load="getDataList"
               @refresh-change="refreshChange"
    >
      <template slot-scope="scope" slot="isActivity">
        <el-tag v-if="scope.row.isActivity === 1" type="success" size="small">{{$i18n.t('webInfoConfig.open')}}</el-tag>
        <el-tag v-else size="small">{{$i18n.t('webInfoConfig.close')}}</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menuLeft">
        <el-button v-if="isAuth('sys:webConfig:save')"
                   icon="el-icon-plus"
                   type="primary"
                   size="small"
                   @click="addOrUpdateHandle(configType)">
                   {{$i18n.t('brand.add')}}
        </el-button>
      </template>
      <template slot-scope="scope"
                slot="menu">
        <el-button v-if="isAuth('sys:webConfig:update')"
                   icon="el-icon-edit"
                   size="small"
                   type="primary"
                   @click="addOrUpdateHandle(configType, scope.row.id)">
                   {{$i18n.t('brand.revise')}}
        </el-button>
        <el-button v-if="isAuth('sys:webConfig:delete')"
                   type="danger"
                   icon="el-icon-delete"
                   size="small"
                   @click="deleteHandle(scope.row.id)">
                   {{$i18n.t('brand.delete')}}
        </el-button>
      </template>

    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/sys/webConfig-bs'
import AddOrUpdate from './webConfig-add-or-update'

export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {}, // 搜索
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false,
      configType: 1 // 配置类型（1:后台 2:PC端 3:h5端）
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/webConfig/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              configType: this.configType
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (configType, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(configType, id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('seckill.isDeleOper'), this.$i18n.t('remindPop.operation'), {
        confirmButtonText: this.$i18n.t('remindPop.confirm'),
        cancelButtonText: this.$i18n.t('remindPop.cancel'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/webConfig/operate/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('remindPop.succeeded'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => {
      })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    }
  }
}
</script>
<style lang="scss">
.el-table__fixed::before ,.el-table__fixed-right::before{
  height: 0;
}
</style>
